package com.meta.builder.schema.config;

import cn.hutool.core.lang.func.LambdaUtil;
import com.alibaba.fastjson2.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.JsonNode;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JFieldVar;
import lombok.*;
import lombok.experimental.Accessors;
import org.jsonschema2pojo.AbstractTypeInfoAwareAnnotator;
import org.jsonschema2pojo.GenerationConfig;

@Setter
@Getter
public class MetaAbstractTypeInfoAnnotator extends AbstractTypeInfoAwareAnnotator {

    /**
     * 是否开启使用lombok注解
     */
    boolean lombokAnnotFlag = true;

    /**
     * 是否开启使用swagger注解
     */
    boolean swaggerAnnotFlag = true;

    /**
     * 是否开启使用mybatisplus注解
     */
    boolean mybatisPlusAnnotFlag = true;


    /**
     * 是否开启使用jackson注解
     */
    boolean jacksonAnnotFlag = false;


    public MetaAbstractTypeInfoAnnotator(GenerationConfig generationConfig) {
        super(generationConfig);
    }

    @Override
    protected void addJsonTypeInfoAnnotation(JDefinedClass clazz, String propertyName) {
    }

    /**
     * 类上的注解
     */
    @Override
    public void propertyInclusion(JDefinedClass clazz, JsonNode schema) {
        if (lombokAnnotFlag) {
            clazz.annotate(Data.class);
            clazz.annotate(Builder.class);
            clazz.annotate(NoArgsConstructor.class);
            clazz.annotate(AllArgsConstructor.class);
            clazz.annotate(Accessors.class).param(LambdaUtil.getMethodName(Accessors::chain), true);
        }
        if (mybatisPlusAnnotFlag) {
//            clazz.annotate(TableName.class).param(LambdaUtil.getMethodName(TableName::value), StrUtil.toUnderlineCase(clazz.name()));
        }
        if (swaggerAnnotFlag) {
//            clazz.annotate(ApiModel.class).param(LambdaUtil.getMethodName(ApiModel::description), clazz.name());
        }

    }


    /**
     * 属性字段上的注解
     */
    @Override
    public void propertyField(JFieldVar field, JDefinedClass clazz, String propertyName, JsonNode propertyNode) {
        if (mybatisPlusAnnotFlag) {
//            field.annotate(TableField.class).param("value", StrUtil.toUnderlineCase(propertyName));
        }
        if (swaggerAnnotFlag) {
//            field.annotate(ApiModelProperty.class).param("value", propertyName);
        }
        if (jacksonAnnotFlag) {
            field.annotate(JsonProperty.class).param("value", propertyName);
        }
        field.annotate(JSONField.class).param("name", propertyName);
    }
}

